﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace NetAcademia.Web.Mvc.ImageRendering
{
    public class DataObject : ICustomTypeDescriptor
    {
        protected Dictionary<string, object> properties;

        public DataObject() : this(null) { }
        public DataObject(Dictionary<string, object> properties)
        {
            if (properties == null)
                this.properties = new Dictionary<string, object>();
            else
                this.properties = properties;
        }

        public void AddProperty(string name, object value)
        {
            properties.Add(name, value);
        }

        #region ICustomTypeDescriptor Members

        public System.ComponentModel.AttributeCollection GetAttributes()
        {
            return System.ComponentModel.AttributeCollection.Empty;
        }

        public string GetClassName()
        {
            return "NetAcademia.Web.Handlers.DataObject";
        }

        public string GetComponentName()
        {
            throw new NotImplementedException();
        }

        public TypeConverter GetConverter()
        {
            return null;
        }

        public EventDescriptor GetDefaultEvent()
        {
            return null;
        }

        public PropertyDescriptor GetDefaultProperty()
        {
            return null;
        }

        public object GetEditor(Type editorBaseType)
        {
            return null;
        }

        public EventDescriptorCollection GetEvents(Attribute[] attributes)
        {
            return EventDescriptorCollection.Empty;
        }

        public EventDescriptorCollection GetEvents()
        {
            return EventDescriptorCollection.Empty;
        }

        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            return GetProperties();
        }

        public PropertyDescriptorCollection GetProperties()
        {
            List<DataPropertyDescriptor> retcoll = new List<DataPropertyDescriptor>();
            foreach (var item in properties)
            {
                retcoll.Add(new DataPropertyDescriptor(item.Key, item.Value.GetType()));
            }

            return new PropertyDescriptorCollection(retcoll.ToArray());
        }

        public object GetPropertyOwner(PropertyDescriptor pd)
        {
            return this;
        }

        #endregion

        public object GetValue(string name)
        {
            return properties[name];
        }



    }

    public class DataPropertyDescriptor : PropertyDescriptor
    {
        Type propertyType;
        public DataPropertyDescriptor(string name, Type propertyType)
            : base(name, new Attribute[0])
        {
            this.propertyType = propertyType;
        }

        public override bool CanResetValue(object component)
        {
            return false;
        }

        public override Type ComponentType
        {
            get { return typeof(DataObject); }
        }

        public override object GetValue(object component)
        {
            DataObject data = (DataObject)component;
            return data.GetValue(this.Name);
        }

        public override bool IsReadOnly
        {
            get { return true; }
        }

        public override Type PropertyType
        {
            get { return this.propertyType; }
        }

        public override void ResetValue(object component)
        {
            throw new NotImplementedException();
        }

        public override void SetValue(object component, object value)
        {
            throw new NotImplementedException();
        }

        public override bool ShouldSerializeValue(object component)
        {
            return false;
        }
    }
}
